# -*- coding: utf-8 -*-
import pymongo

from pymongo import MongoClient

from hssplider import database


class mongo:
    def __init__(self):
        con = MongoClient(database.HOST, database.PORT)
        self.dsn = con
        self.dsn = self.dsn[database.DB_NAME]

    def insert(self, collection, data):
        dsn = self.dsn[collection]
        dsn.insert(data)

    def insert_more(self, collection, data):
        dsn = self.dsn[collection]
        dsn.insert_many(data)

    def find_one(self, collection, data):
        dsn = self.dsn[collection]
        result = dsn.find_one(data)
        return result

    def find(self, collection, data):
        dsn = self.dsn[collection]
        result = dsn.find(data)
        return result

    def findall(self, collection):
        dsn = self.dsn[collection]
        result = dsn.find()
        return result

    def update(self, collection, condition, data):
        dsn = self.dsn[collection]
        result = dsn.update(condition, data)
        return result
